import Vue from 'vue'
import App from './App'
import store from './store';
import { BASE_URL } from '@/common/api/auth.js';

import cuCustom from './components/cu-custom.vue'
Vue.component('cu-custom',cuCustom)

import login from './components/login.vue'
Vue.component('login', login)

App.mpType = 'app'

Vue.prototype.$store = store;
Vue.prototype.base_url = BASE_URL;


const msg = (title, duration = 2000, mask = false, icon = 'none') => {
	if (Boolean(title) === false) return;
	uni.showToast({ title, duration, mask, icon });
}

const isLogin = ()=>{
	return uni.getStorageSync('token')?true:false;
}

const to = (url, animationType = 'pop-in', animationDuration = 300) => {
	if(url == '/pages/tabbar/index' || url == '/pages/tabbar/user' || url == '/pages/tabbar/product' || url == '/pages/tabbar/reserve') {
		return uni.switchTab({
			url,
			animationType,
			animationDuration
		})
	}
	uni.navigateTo({
		url,
		animationType,
		animationDuration
	})
}

const auth = (url, animationType = 'pop-in', animationDuration = 300) => {
	if(!isLogin()) return store.dispatch('user/setLogin',true)
	to(url, animationType, animationDuration)
}

const back = () => {
	if(getCurrentPages().length <= 1) 
		return to('/pages/tabbar/index');
	uni.navigateBack({
		delta: 1
	});
}

const phoneCall = (mobile) => {
	uni.makePhoneCall({
		phoneNumber: mobile,
		fail(res) {
			copyText(mobile);
			msg('电话号码已复制！')
		}
	})
}

const copyText = (text) => {
	uni.setClipboardData({
		data: text,
		success: function () {
			msg('复制成功！')
		},
	});
}

const showLoad = (title, mask=true) =>{
	uni.showLoading({ title: title, mask: mask });
}

const showModal = (content, callback, title = "提示", confirmText = "确定", cancelText = "取消") => {
	uni.showModal({
		title: title,
		content: content,
		confirmText: confirmText,
		cancelText: cancelText,
		confirmColor: '#0080cc',
		cancelColor: '#576B95',
		success(res) {
			if(res.confirm) 
				callback&&callback(res.content);
		}
	})
}

const timeFormat = (timestamp = null, fmt = 'yyyy-mm-dd') => {
	// 其他更多是格式化有如下:
	// yyyy:mm:dd|yyyy:mm|yyyy年mm月dd日|yyyy年mm月dd日 hh时MM分等,可自定义组合
	timestamp = parseInt(timestamp);
	// 如果为null,则格式化当前时间
	if (!timestamp) timestamp = Number(new Date());
	// 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位)
	if (timestamp.toString().length == 10) timestamp *= 1000;
	let date = new Date(timestamp);
	let ret;
	let opt = {
		"y+": date.getFullYear().toString(), // 年
		"m+": (date.getMonth() + 1).toString(), // 月
		"d+": date.getDate().toString(), // 日
		"h+": date.getHours().toString(), // 时
		"M+": date.getMinutes().toString(), // 分
		"s+": date.getSeconds().toString() // 秒
		// 有其他格式化字符需求可以继续添加，必须转化成字符串
	};
	for (let k in opt) {
		ret = new RegExp("(" + k + ")").exec(fmt);
		if (ret) {
			fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]<10?'0'+opt[k]:opt[k]) : (opt[k].padStart(ret[1].length, "0")))
		};
	};
	return fmt;
}

Vue.prototype.$onLaunched = new Promise(resolve => {
	Vue.prototype.$isResolve = resolve;
});

Vue.prototype.$tools = {
	to,
	msg,
	auth,
	back,
	isLogin,
	copyText,
	showLoad,
	showModal,
	phoneCall,
	timeFormat,
}

const app = new Vue({
    ...App,
	store
})
app.$mount()